package com.yytek.upgrade.controller;

import java.util.List;
import java.io.IOException;

import com.yytek.common.core.domain.R;
import com.yytek.common.security.utils.SecurityUtils;
import com.yytek.upgrade.domain.UpgradeFirmware;
import com.yytek.upgrade.dto.UpgeadeStrategyDevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.upgrade.domain.UpgradeStrategy;
import com.yytek.upgrade.service.IUpgradeStrategyService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 升级策略Controller
 * 
 * @author yytek-iot
 * @date 2022-09-06
 */
@Api(value = "升级策略控制器", tags = {"升级策略管理"})
@RestController
@RequestMapping("/strategy")
public class UpgradeStrategyController extends BaseController
{
    @Resource
    private IUpgradeStrategyService upgradeStrategyService;

    /**
     * 分页查询升级策略列表
     */
    @ApiOperation("分页查询升级策略列表")
    @RequiresPermissions("upgrade:strategy:list")
    @GetMapping("/list")
    public TableDataInfo list(UpgradeStrategy upgradeStrategy)
    {
        //获取当前登录人创建者
        Long userId = SecurityUtils.getUserId();
        upgradeStrategy.setCreateBy(String.valueOf(userId));
        startPage();
        List<UpgradeStrategy> list = upgradeStrategyService.selectUpgradeStrategyList(upgradeStrategy);
        return getDataTable(list);
    }

    /**
     * 导出升级策略列表
     */
    @ApiOperation("导出升级策略列表")
    @RequiresPermissions("upgrade:strategy:export")
    @Log(title = "升级策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UpgradeStrategy upgradeStrategy)
    {
        List<UpgradeStrategy> list = upgradeStrategyService.selectUpgradeStrategyList(upgradeStrategy);
        ExcelUtil<UpgradeStrategy> util = new ExcelUtil<UpgradeStrategy>(UpgradeStrategy.class);
        util.exportExcel(response, list, "升级策略数据");
    }

    /**
     * 根据策略id获取升级包信息
     */
    @ApiOperation("根据策略id获取升级包信息")
    @PostMapping(value = "/getUpgradeFileByStrategyId")
    public R<UpgradeFirmware> getUpgradeFileByStrategyId(Long id)
    {
        return R.ok(upgradeStrategyService.getUpgradeFileByStrategyId(id));
    }

    /**
     * 获取升级策略详细信息api
     */
    @ApiOperation("获取升级策略详细信息api")
    @PostMapping(value = "/getUpgradeStrategy")
    public R<UpgradeStrategy> getUpgradeStrategy(Long id)
    {
        return R.ok(upgradeStrategyService.selectUpgradeStrategyById(id));
    }

    /**
     * 获取升级策略详细信息
     */
    @ApiOperation("获取升级策略详细信息")
    @RequiresPermissions("upgrade:strategy:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(upgradeStrategyService.selectUpgradeStrategyById(id));
    }

    /**
     * 新增升级策略和绑定设备
     */
    @ApiOperation("新增升级策略和绑定设备")
    @RequiresPermissions("upgrade:strategy:add")
    @Log(title = "新增升级策略和绑定设备", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addAndBindDevice")
    public AjaxResult addAndBindDevice(@RequestBody UpgeadeStrategyDevice upgeadeStrategyDevice)
    {
        return toAjax(upgradeStrategyService.addAndBindDevice(upgeadeStrategyDevice));
    }

    /**
     * 新增升级策略和关联设备关系
     */
    @ApiOperation("新增升级策略")
    @RequiresPermissions("upgrade:strategy:add")
    @Log(title = "升级策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UpgradeStrategy upgradeStrategy)
    {
        return toAjax(upgradeStrategyService.insertUpgradeStrategy(upgradeStrategy));
    }

    /**
     * 修改升级策略
     */
    @ApiOperation("修改升级策略")
    @RequiresPermissions("upgrade:strategy:edit")
    @Log(title = "升级策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UpgradeStrategy upgradeStrategy)
    {
        return toAjax(upgradeStrategyService.updateUpgradeStrategy(upgradeStrategy));
    }

    /**
     * 删除升级策略
     */
    @ApiOperation("删除升级策略")
    @RequiresPermissions("upgrade:strategy:remove")
    @Log(title = "升级策略", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(upgradeStrategyService.deleteUpgradeStrategyByIds(ids));
    }
}
